<template>
	<view>
		<uni-nav-bar :statusBar="true" dark title="选择球队" fixed left-icon="left" @clickLeft="clickLeft">
		</uni-nav-bar>
		<view>
			<uni-search-bar clearButton="none" cancelButton="none" @confirm="search" @input="input"></uni-search-bar>
		</view>
		<template v-if="list.length">
			<view class="list" v-for="(item,index) in list" :key="index" @click="onSelect(item)">
				<image v-if="item.teamLogo" :src="imageUrl+item.teamLogo" mode=""></image>
				<image src="@/static/user.jpg" v-else mode=""></image>
				<view>
					{{item.teamName}}
				</view>
			</view>
			<uni-load-more :status="loadingType"></uni-load-more>
		</template>

		<view class="noneView" v-if="!list.length && !loading">
			<image src="@/static/images/basketball.png" mode=""></image>
			<view>联赛还没有球队</view>
			<button class="btn" type="default" @click="onInvitation">点击邀请球队加入</button>
		</view>
	</view>
</template>

<script>
	import {
		getImage,
		getbasketballTeamList,
	} from '@/api/basketballTeam/index.js'
	import {
		imageUrl
	} from '@/config.js'
	export default {
		data() {
			return {
				list: [],
				loading: true,
				imageUrl,
				pageData: {
					pageSize: 10,
					pageNum: 1
				},
				total: 0,
				loadingType: 'more'
			}
		},
		methods: {
			clickLeft() {
				uni.navigateBack()
			},
			onInvitation() {
				uni.navigateTo({
					url: '/pages/basketballTeam/list'
				})
				// uni.navigateTo({
				// 	url: '/pages/competition/Invitation'
				// })
			},
			onSelect(item) {
				// 选择球队
				if (this.type == 'one') {
					uni.setStorageSync('onebasketballTeam', item);
				} else {
					uni.setStorageSync('twobasketballTeam', item);
				}

				uni.navigateBack()
			},
			search(val) {
				console.log(val)
				this.pageData = {
					pageNum: 1,
					pageSize: 12
				}
				this.list = []
				this.getList(val.value);
			},
			getList(teamName = '') {
				let leagueId = uni.getStorageSync('contestId')
				this.loading = true
				getbasketballTeamList({
					...this.pageData,
					leagueId: leagueId,
					id: this.type == 'two' ? uni.getStorageSync('onebasketballTeam').id : uni.getStorageSync(
						'twobasketballTeam').id,
					teamName,
				}).then(res => {
					this.loading = false
					this.total = res.total
					this.list.push(...res.rows)
					if (this.list.length == this.total) {
						this.loadingType = 'no-more'
						return;
					}
				})
			}
		},
		async onReachBottom() {
			if (this.list.length == this.total) {
				this.loadingType = 'no-more'
				return;
			}
			this.loadingType = 'loading'
			this.pageData.pageNum += 1
			this.getList()
		},
		onLoad({
			type
		}) {
			this.type = type
		},
		onShow() {
			this.pageData = {
				pageSize: 12,
				pageNum: 1
			}
			this.list = []
			this.getList();
		}
	}
</script>

<style scoped lang="scss">
	.noneView {
		display: flex;
		flex-direction: column;
		padding: 30rpx 30rpx;
		align-items: center;
		padding-top: 40%;

		image {
			width: 140rpx;
			height: 140rpx;
		}

		.btn {
			margin-top: 30rpx;
			width: 100%;
			color: #fff;
			background-color: $btn-bg-color;
		}
	}

	.list {
		display: flex;
		align-items: center;
		border-bottom: 1px solid #ddd;
		padding: 20rpx 30rpx;
		font-size: 28rpx;
		font-weight: 600;

		image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 100%;
			margin-right: 30rpx;
		}
	}
</style>